package com.wh.testSiLock;

import java.util.concurrent.TimeUnit;

public class SiLockDemo1 {
    public static void main(String[] args) {
        String lockA = "LockA";
        String lockB = "LockB";
        new Thread(new MyThread(lockA, lockB), "A").start();
        new Thread(new MyThread(lockB, lockA), "B").start();
    }
}

class MyThread implements Runnable {

    private String LockA;
    private String LockB;

    public MyThread(String lockA, String lockB) {
        this.LockA = lockA;
        this.LockB = lockB;
    }

    @Override
    public void run() {
        synchronized (LockA) {
            System.out.println(Thread.currentThread().getName() + "--lock:" + LockA + "===>get" + LockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LockB) {
                System.out.println(Thread.currentThread().getName() + "--lock:" + LockB + "===>get" + LockA);
            }
        }
    }
}